/**
 *******************************************************************************
 * @FileName  : tlsr825x_wdg.c
 * @Author    : GaoQiu
 * @CreateDate: 2020-09-10
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 *
 *******************************************************************************
 *
 * The information contained herein is confidential and proprietary property of
 * GaoQiu and is available under the terms of Commercial License Agreement
 * between GaoQiu and the licensee in separate contract or the terms described
 * here-in.
 *
 * This heading MUST NOT be removed from this file.
 *
 * Licensees are granted free, non-transferable use of the information in this
 * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
 *
 *******************************************************************************
 */
#include "tlsr825x_wdg.h"

/**
 * @brief : Watchdog initialization.
 * @param : periodMs
 * @param : sysClkFreq_MHz      system clock frequecy.
 * @return: none.
 */
void WDG_Init(uint32_t periodMs, uint32_t sysClkFreq_MHz)
{
	uint32_t tick = periodMs * 1000 * sysClkFreq_MHz;
	tick >>= 18;

	uint32_t tmp = TIM->CTRL_STAT;
	tmp &= ~(0xFF000000 | TIM_CTRL_WDG_CAP);
	tmp |= (tick<<9);

	TIM->CTRL_STAT = tmp;
}

/**
 * @brief : Watchdog enable/disable.
 * @param : enable       can be true/false.
 * @return: none.
 */
void WDG_Cmd(bool_t enable)
{
	if(enable)
	{
		TIM->CTRL_STAT |= TIM_CTRL_WDG_EN;
	}
	else{
		TIM->CTRL_STAT &= ~TIM_CTRL_WDG_EN;
	}
}

/**
 * @brief : Watchdog feed dog.
 * @param : none.
 * @return: none.
 */
void WDG_FeedDog(void)
{
	 uint8_t *pState = (uint8_t *)&TIM->CTRL_STAT + 3;
	 *pState |= 0x08;
}

